//
//  BXPersonDataRecordVoiceView.m
//  BXlive
//
//  Created by mac on 2020/8/25.
//  Copyright © 2020 cat. All rights reserved.
//

#import "BXPersonDataRecordVoiceView.h"
#import "DynRecoderVoice.h"
#import "DynPlayMp3Voice.h"
#import "BXDynAlertRemoveSoundView.h"
#import "BXDynCircleProgressView.h"
#import <Masonry/Masonry.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <YYCategories/YYCategories.h>
#import <MMKV/MMKV.h>

@interface BXPersonDataRecordVoiceView()<playDelegate>
@property(nonatomic, strong)DynRecoderVoice *recoderSound;
@property(nonatomic, strong)DynPlayMp3Voice *playSound;
@property (nonatomic,strong)NSTimer *recordTimer;

@property(nonatomic, strong)UIView *micrBackView;
@property(nonatomic, strong)UIImageView *MicrImageView;

@property(nonatomic, strong)UIButton *micrAniImageView;
@property(nonatomic, strong)UIButton *micrAniPlaybtn;
@property(nonatomic, strong)UILabel *micrAnilabel;
@property(nonatomic, strong)UIButton *micrdeleteBtn;
@property(nonatomic, strong)UIButton *micrSaveimageBtn;


@property(nonatomic, strong)BXDynCircleProgressView *circleView;
@property(nonatomic, strong)UILabel *Duralabel;
@property (strong, nonatomic) NSString *mp3Path;
@property (assign, nonatomic) NSInteger timeRecoder;
@property (assign, nonatomic) NSInteger initRecoder;
@end
@implementation BXPersonDataRecordVoiceView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _recoderSound = [[DynRecoderVoice alloc]init];
        _playSound = [[DynPlayMp3Voice alloc]init];
        _playSound.delegate = self;
        [self VoiceBackView];
    }
    return self;
}
-(void)VoiceBackView{
    

    _micrBackView = [[UIView alloc]init];
//    _micrBackView.backgroundColor = [UIColor cyanColor];
    [self addSubview:_micrBackView];
    [_micrBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(250);
        make.bottom.mas_offset(0);
    }];
    
    _circleView = [[BXDynCircleProgressView alloc]initWithFrame:CGRectMake(_micrAniPlaybtn.center.x, _micrAniPlaybtn.center.y, 92, 92)];
    [_micrBackView addSubview:_circleView];
    
    [_circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.micrBackView.mas_centerY).offset(30);
        make.centerX.mas_equalTo(_micrBackView.mas_centerX);
        make.width.height.mas_equalTo(92);
    }];
    
    _micrAniImageView = [UIButton buttonWithType:UIButtonTypeCustom];
    [_micrAniImageView setImage:CImage(@"dyn_issue_LuYin_will") forState:UIControlStateNormal];
    [_micrBackView addSubview:_micrAniImageView];
    [_micrAniImageView addTarget:self action:@selector(recoderAct:) forControlEvents:UIControlEventTouchUpInside];
    [_micrAniImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.micrBackView.mas_centerY).offset(30);
        make.centerX.mas_equalTo(_micrBackView.mas_centerX);
        make.width.height.mas_equalTo(84);
    }];
    
    _micrAniPlaybtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_micrAniPlaybtn setImage:CImage(@"dyn_issue_LuYin_play") forState:UIControlStateNormal];
    [_micrBackView addSubview:_micrAniPlaybtn];
    [_micrAniPlaybtn addTarget:self action:@selector(PlayAct:) forControlEvents:UIControlEventTouchUpInside];
    [_micrAniPlaybtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.micrBackView.mas_centerY).offset(30);
        make.centerX.mas_equalTo(_micrBackView.mas_centerX);
        make.width.height.mas_equalTo(84);
    }];
    _micrAniPlaybtn.hidden = YES;
    
    
    
    
    _micrAnilabel = [[UILabel alloc]init];
    _micrAnilabel.text = NSLocalizedString(@"点击录音", nil);
    _micrAnilabel.textAlignment = 1;
    _micrAnilabel.textColor = sl_textSubColors;
    _micrAnilabel.font = [UIFont systemFontOfSize:12];
    [_micrBackView addSubview:_micrAnilabel];
    [_micrAnilabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(_micrAniImageView.mas_top).offset(-10);
        make.width.mas_equalTo(84);
        make.height.mas_equalTo(20);
    }];
    
    _Duralabel = [[UILabel alloc]init];
    _Duralabel.text = @"00:00";
    _Duralabel.textAlignment = 1;
    _Duralabel.textColor = sl_textSubColors;
    _Duralabel.font = [UIFont systemFontOfSize:12];
    [_micrBackView addSubview:_Duralabel];
    [_Duralabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(_micrAnilabel.mas_top).offset(-10);
        make.width.mas_equalTo(84);
        make.height.mas_equalTo(20);
    }];
    _Duralabel.hidden = YES;
    
    _micrdeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_micrdeleteBtn setImage:CImage(@"dyn_issue_LuYin_del") forState:UIControlStateNormal];
    [_micrdeleteBtn addTarget:self action:@selector(DelAct:) forControlEvents:UIControlEventTouchUpInside];
    [_micrBackView addSubview:_micrdeleteBtn];
    [_micrdeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_micrAniPlaybtn.mas_centerY);
        make.right.mas_equalTo(_micrAniPlaybtn.mas_left).offset(-30);
        make.width.height.mas_equalTo(26);
    }];
    
    _micrdeleteBtn.hidden = YES;
    
    _micrSaveimageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_micrSaveimageBtn addTarget:self action:@selector(SaveAct:) forControlEvents:UIControlEventTouchUpInside];
    [_micrSaveimageBtn setImage:CImage(@"dyn_issue_LuYin_save") forState:UIControlStateNormal];
    [_micrBackView addSubview:_micrSaveimageBtn];
    [_micrSaveimageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_micrAniPlaybtn.mas_centerY);
        make.left.mas_equalTo(_micrAniPlaybtn.mas_right).offset(30);
        make.width.height.mas_equalTo(26);
    }];
    _micrSaveimageBtn.hidden = YES;
}
-(void)recoderAct:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        
        [_recoderSound recordStart];
        [_micrAniImageView setImage:CImage(@"dyn_issue_LuYin_stop") forState:UIControlStateNormal];
        _Duralabel.hidden = NO;
        
        [self startRecordTimer];
        WS(weakSelf);
        _recoderSound.GetRecoder = ^(BOOL isRecoder) {
            if (!isRecoder) {
                [weakSelf stopRecordTimer];
                weakSelf.micrAnilabel.text = NSLocalizedString(@"点击录音", nil);
                [weakSelf.micrAniImageView setImage:CImage(@"dyn_issue_LuYin_will") forState:UIControlStateNormal];
                weakSelf.Duralabel.hidden = YES;
            }
        };
        _recoderSound.GetMp3Path = ^(NSString * _Nonnull Mp3Path) {
            weakSelf.mp3Path = Mp3Path;
            weakSelf.micrAniImageView.hidden = YES;
            weakSelf.micrAniPlaybtn.hidden = NO;
            weakSelf.micrAnilabel.text = NSLocalizedString(@"点击播放", nil);
            [weakSelf.micrAniPlaybtn setImage:CImage(@"dyn_issue_LuYin_play") forState:UIControlStateNormal];
            weakSelf.micrdeleteBtn.hidden = NO;
            weakSelf.micrSaveimageBtn.hidden = NO;
        };
    }else{
        [_micrAniImageView setImage:CImage(@"dyn_issue_LuYin_will") forState:UIControlStateNormal];
        _micrAnilabel.text = NSLocalizedString(@"点击录音", nil);
        [_recoderSound recordEnd];
        [self stopRecordTimer];
        
    }
}
-(void)PlayAct:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        [self startPlayTimer];
        [_playSound StartPlayWithplaypath:_mp3Path];
        [_playSound startPlay];
        [_micrAniPlaybtn setImage:CImage(@"dyn_issue_LuYin_stop") forState:UIControlStateNormal];
    }else{
        [self stopPlayTimer];
        [_playSound StopPlay];
        [_micrAniPlaybtn setImage:CImage(@"dyn_issue_LuYin_play") forState:UIControlStateNormal];
    }
}

-(void)DelAct:(UIButton *)btn{
    BXDynAlertRemoveSoundView *view =[[BXDynAlertRemoveSoundView alloc]initWithFrame:CGRectMake(0, 0, __kWidth, __kHeight)Title:NSLocalizedString(@"是否放弃当前录音", nil) Sure:NSLocalizedString(@"放弃", nil) Cancle:NSLocalizedString(@"取消", nil)];
    WS(weakSelf);
    view.RemoveBlock = ^{
        
        weakSelf.initRecoder = 0;
        weakSelf.timeRecoder = 0;
        weakSelf.micrAniImageView.hidden = NO;
        weakSelf.Duralabel.hidden = YES;
        weakSelf.micrAniPlaybtn.hidden = YES;
        weakSelf.micrAnilabel.text = NSLocalizedString(@"点击录音", nil);
        [weakSelf.micrAniImageView setImage:CImage(@"dyn_issue_LuYin_will") forState:UIControlStateNormal];
        weakSelf.micrdeleteBtn.hidden = YES;
        weakSelf.micrSaveimageBtn.hidden = YES;
        [weakSelf.circleView stopTimer];
    };
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:view];

}
-(void)SaveAct:(UIButton *)btn{
    
    _micrAniImageView.hidden = NO;
    _micrAniPlaybtn.hidden = YES;
    _Duralabel.hidden = YES;
    _micrAnilabel.text = NSLocalizedString(@"点击录音", nil);
    [_micrAniImageView setImage:CImage(@"dyn_issue_LuYin_will") forState:UIControlStateNormal];
    _micrdeleteBtn.hidden = YES;
    _micrSaveimageBtn.hidden = YES;
    _initRecoder = 0;
    _timeRecoder = 0;
    if (self.ReturnPath) {
        self.ReturnPath(_mp3Path);
    }

    

}


#pragma mark - Private Methods

- (void)startRecordTimer{
    _timeRecoder = 0;
    if (!self.recordTimer) {
        self.recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(recoderTime) userInfo:nil repeats:YES];
        [self.recordTimer setFireDate:[NSDate distantPast]];
        [_circleView setTotalSecondTime:60];
//        [_circleView setTotalMinuteTime:60];
        [_circleView startTimer];
    }
}

- (void)stopRecordTimer{
    if (self.recordTimer) {
        [self.recordTimer invalidate];
        self.recordTimer = nil;
        _initRecoder = _timeRecoder;

    }
    [_circleView stopTimer];
}
-(void)recoderTime{
    _timeRecoder++;
    _Duralabel.text = [self getFormatString:_timeRecoder];
    if (_timeRecoder >= 60) {
        [self recoderAct:_micrAniImageView];
    }
}


- (void)startPlayTimer{
    if (!self.recordTimer) {
        self.recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(PlayTime) userInfo:nil repeats:YES];
        [self.recordTimer setFireDate:[NSDate distantPast]];
    }
    [_circleView setTotalSecondTime:_initRecoder];
    [_circleView startTimer];
}

- (void)stopPlayTimer{
    if (self.recordTimer) {
        [self.recordTimer invalidate];
        self.recordTimer = nil;
        _timeRecoder = _initRecoder;
        _Duralabel.text = [self getFormatString:_timeRecoder];

    }
     [_circleView stopTimer];
}
-(void)PlayTime{
    _timeRecoder--;
    if (_timeRecoder >=0) {
        _Duralabel.text = [self getFormatString:_timeRecoder];
    }
}
- (NSString *)getFormatString:(NSInteger)totalSeconds {
    NSInteger seconds = totalSeconds % 60;
    NSInteger minutes = (totalSeconds / 60) % 60;
    NSInteger hours = totalSeconds / 3600;
    if (hours <= 0) {
        return [NSString stringWithFormat:@"%02ld:%02ld",(long)minutes, (long)seconds];
    }
    return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)hours, (long)minutes, (long)seconds];
}
#pragma - mark PlayDelegate
-(void)endPlaySound{
    [self stopPlayTimer];
    [_playSound StopPlay];
    [_micrAniPlaybtn setImage:CImage(@"dyn_issue_LuYin_play") forState:UIControlStateNormal];
    [self PlayAct:_micrAniPlaybtn];
}
-(void)saveBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;

    MMKV *mmkv = [MMKV defaultMMKV];
    [mmkv setBool:sender.selected forKey:@"kSaveKey"];
    [mmkv sync];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
